package org.primefaces.examples.moviecollector.dao;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.primefaces.examples.moviecollector.domain.Eventos;
import org.springframework.stereotype.Repository;



@Repository
public class EventoDAOWithJPA  implements EventoDAO {
	
	@PersistenceContext
	protected EntityManager entityManager;

	public EntityManager getEntityManager() {
		return entityManager;
	}


	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}


	@SuppressWarnings("unchecked")
	public List<Eventos> findByNombre(String nombre) {
		Query query = entityManager.createQuery("Select s from Eventos s where s.nombre LIKE :nombre");
		query.setParameter("nombre", nombre + "%");

		return query.getResultList();
	}
	
	
	public Eventos loadById(Long id) {
		return entityManager.find(Eventos.class, id);
	}

	public void persist(Eventos evet) {
		entityManager.persist(evet);
		
	}

	public void update(Eventos evet) {
		entityManager.merge(evet);
		
	}

	public void delete(Eventos evet) {
		entityManager.createQuery("DELETE FROM Eventos s WHERE s.id=:id").setParameter("id", evet.getId()).executeUpdate();
		
	}

	public List<Eventos> loadAll() {
		return entityManager.createQuery("Select s from Eventos s").getResultList();
	}

	

	public List<Eventos> loadLazy(int first, int pageSize,
			String sortField, String sortOrder, Map<String, String> filters) {
		// TODO Auto-generated method stub
		return null;
	}

}
